from apsoo.hotel.models import *
from django.contrib import admin
from django import forms
from apsoo.hotel.views import *

class HospedeAdmin(admin.ModelAdmin):
	pass
admin.site.register(Hospede, HospedeAdmin)

class CheckRes(forms.ModelForm):
	class Meta:
		model = Reserva

	def clean(self):
		cleaned_data = self.cleaned_data
		idApartamento = cleaned_data.get("idApartamento")
		dataInicio = cleaned_data.get("entradaPrevista")
		dataFim = cleaned_data.get("saidaPrevista")
		print(idApartamento)
		print(dataInicio)
		print(dataFim)
		print(view5(idApartamento, dataInicio, dataFim ))
		print(view1(idApartamento, dataInicio, dataFim ))
		if view5(idApartamento, dataInicio, dataFim ) and view1(idApartamento, dataInicio, dataFim ):
			return self.cleaned_data
		else: 
			#aps = view2(dataInicio, dataFim )
			#result = ""
			#for ap in aps :
			#	result = result + str(ap.id) + "\n"
			raise forms.ValidationError('Nao foi possivel fazer a reserva. ')

class ReservaAdmin(admin.ModelAdmin):
	form = CheckRes
	pass
admin.site.register(Reserva, ReservaAdmin)

class ApartamentoAdmin(admin.ModelAdmin):
	pass
admin.site.register(Apartamento, ApartamentoAdmin)

class TipoApartamentoAdmin(admin.ModelAdmin):
	pass
admin.site.register(TipoApartamento, TipoApartamentoAdmin)

class TipoServicoAdmin(admin.ModelAdmin):
	pass
admin.site.register(TipoServico, TipoServicoAdmin)

class CheckIn(forms.ModelForm):
	class Meta:
		model = HistoricoOcupacao

	def clean(self):
		cleaned_data = self.cleaned_data
		idHospede = cleaned_data.get("idHospede")
		idApartamento = cleaned_data.get("idApartamento")
		dataInicio = cleaned_data.get("dataInicio")
		dataFim = cleaned_data.get("dataFim")
		print(idApartamento)
		print(dataInicio)
		print(dataFim)
		print(view5(idApartamento, dataInicio, dataFim ))
		print(view1(idApartamento, dataInicio, dataFim ))
		if view5(idApartamento, dataInicio, dataFim ) and view6(idApartamento, dataInicio, dataFim ,idHospede):
			return self.cleaned_data
		else: 
			raise forms.ValidationError('Nao foi possivel fazer o check-in.')

class HistoricoOcupacaoAdmin(admin.ModelAdmin): 
	form = CheckIn

	def add_view(self,request):
		result = super(HistoricoOcupacaoAdmin, self).add_view(request)
		q1 = HistoricoOcupacao.objects.all().values('idHospede')
		q2 = HistoricoOcupacao.objects.all().values('idApartamento')
		q3 = HistoricoOcupacao.objects.all().values('dataInicio')
		q4 = HistoricoOcupacao.objects.all().values('dataFim')


		{Reserva.objects
		.filter(idHospede__id__in = q1)
		.filter(idApartamento__id__in = q2)
		.filter(entradaPrevista__in=q3)
		.filter(saidaPrevista__in=q4)
		.delete()
		}
		return result
	
	
	pass
admin.site.register(HistoricoOcupacao, HistoricoOcupacaoAdmin)
	
class HistoricoServicoAdmin(admin.ModelAdmin):
	pass
admin.site.register(HistoricoServico, HistoricoServicoAdmin)

class ServicoAdmin(admin.ModelAdmin):
	pass
admin.site.register(Servico, ServicoAdmin)
